package com.xiangxiao.rpan.gateway.utils;

import com.xiangxiao.rpan.gateway.security.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/7/20 14:44
 */
@Slf4j
public class UrlAccessUtil {
  /**
   * 校验URL是否允许访问
   * @param operationList
   * @param requestUrl
   * @param requestMethod
   * @return
   */
  public static final boolean tryAccess(List<Operation> operationList, String requestUrl, String requestMethod) {
    boolean matches = false;
    for (Operation operation : operationList) {
      if (!StringUtils.equalsIgnoreCase(operation.getMethod(), requestMethod)) {
        continue;
      }

      String targetUrl = operation.getUrl();
      if (StringUtils.equalsIgnoreCase(operation.getUrl(), requestUrl) || requestUrl.startsWith(targetUrl)) {
        matches = true;
        log.info("允许访问url={}, 操作url={}", requestUrl, targetUrl);
        break;
      }
    }

    return matches;
  }
}
